//
//  WBTextView.m
//  新浪微博项目
//
//  Created by neng on 15/10/6.
//  Copyright © 2015年 neng. All rights reserved.
//

#import "WBTextView.h"

#import "WBEmotion.h"
#import "NSString+Emoji.h"
#import "WBEmotionAttachment.h"

@interface WBTextView () <UITextViewDelegate>

//占位label
@property (nonatomic,weak) UILabel *placeholderLabel;

@end

@implementation WBTextView

- (nonnull instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.font = [UIFont systemFontOfSize:15];
        self.placeholderLabel.font = self.font;

    }
    return self;
}

- (void)setHidePlaceholder:(BOOL)hidePlaceholder
{
    _hidePlaceholder = hidePlaceholder;
    
    _placeholderLabel.hidden = hidePlaceholder;
}

//每次设置字体大小都保证占位图片的文字的大小相等
- (void)setFont:(UIFont * __nullable)font
{
    //因为是私有api 写不出_font这样的属性
    [super setFont:font];
    
    self.placeholderLabel.font = font;
    //如果先设置了占位文字再设置文字大小，原本已经算好的尺寸就有可能放不下了，最好在设置完文字大小后再算一次尺寸
    [self.placeholderLabel sizeToFit];
}

//懒加载占位label
- (UILabel *)placeholderLabel
{
    if (_placeholderLabel == nil) {
        
        UILabel *placeholderLabel = [[UILabel alloc] init];
        placeholderLabel.textColor = [UIColor lightGrayColor];
        [self addSubview:placeholderLabel];
        _placeholderLabel = placeholderLabel;
    }
    
    return _placeholderLabel;
}


- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    
    self.placeholderLabel.text = placeholder;
    //设置占位图的尺寸
    [self.placeholderLabel sizeToFit];
}

//插入表情
- (void)insertEmotion:(WBEmotion *)emotion
{

    if (emotion.png) { //是图片表情
        
        //包装图片为附件
        WBEmotionAttachment *att = [[WBEmotionAttachment alloc] init];
        //携带emotion，解析的时候方便使用
        att.emotion = emotion;
        att.image = [UIImage imageNamed:emotion.png];
        //改变附件的大小
        CGFloat attWH = self.font.lineHeight;
        att.bounds = CGRectMake(0, -3, attWH, attWH);
        //创建带附件的属性字符串
        NSAttributedString *imgStr = [NSAttributedString attributedStringWithAttachment:att];
        
        //把textView中的属性字符串转成可变的，用于拼接
        NSMutableAttributedString *attStrM = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
        
        //保存原光标所在位置
        NSInteger loc = self.selectedRange.location;
        
        //替换所有选中的字符串或图片
        [attStrM replaceCharactersInRange:self.selectedRange withAttributedString:imgStr];
        
        //属性文字的大小只能这样修改
        [attStrM addAttribute:NSFontAttributeName value:self.font range:NSMakeRange(0, attStrM.length)];
        
        //把拼接完的字符串替换掉原来的字符串
        self.attributedText = attStrM;
        
        //替换完成把光标移后一个位置
        self.selectedRange = NSMakeRange(loc + 1, 0);
    }else { //emoji
        
        //在光标插入文字，而不是追加文字
        [self insertText:emotion.code.emoji];
    }
    
    //发出文本改变的通知 (完成后再发)
    [[NSNotificationCenter defaultCenter] postNotificationName:UITextViewTextDidChangeNotification object:nil];
}

- (NSString *)fullString
{
    NSMutableString *fullString = [NSMutableString string];
    
    //遍历
    [self.attributedText enumerateAttributesInRange:NSMakeRange(0, self.attributedText.length) options:0 usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) {
        
        //取出附件中的emotion
        WBEmotionAttachment *att = attrs[@"NSAttachment"];
        if (att) { //图片表情
            
            //取出表情模型
            WBEmotion *emotion = att.emotion;
            [fullString appendString:emotion.chs];
            
        }else { //普通文本或者emoji
            
            //获得这个范围的文字
            NSAttributedString *attStr = [self.attributedText attributedSubstringFromRange:range];
            [fullString appendString:attStr.string];
        }
        
    }];
    
    return fullString;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.placeholderLabel.x = 5;
    self.placeholderLabel.y = 8;
}



@end
